/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
import javax.persistence.Query;
import kimo.persistence.controllers.ProducerJpaController;
import kimo.persistence.entities.Producer;
import labels.LabelsEnum;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;

import org.zkoss.zul.Textbox;

/**
 *
 * @author a381u296
 */
public class Search extends SelectorComposer<Component> {

    @Wire
    private Textbox searchTxt;
    @Wire
    private Listbox resultLst;

    private ListModelList<Producer> resultLstLml;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.

        //resultLst.setModel(resultLstLml);
    }

    @Listen("onClick = #searchBtn")
    public void onSearchBtnClick() {
        KimoDAO pdao = new KimoDAO();

        String searchValue = searchTxt.getValue();
        if (!searchValue.equals("")) {
            char zero = searchValue.charAt(0);
            if (Character.isDigit(zero)) { // εχει δωσει ΑΦΜ, λογικα μοναδικο
                Producer producer = pdao.findByAfm(searchValue);
                if (producer != null) {
                    String pdaok = producer.getProducerDaokCd().toString();
                    String udaok = pdao.getUser().getUserDaokCd().toString();
//                    Messagebox.show (pdaok + " vs " + udaok);
                    if (pdaok == udaok) {
                        Executions.sendRedirect("?pid=" + producer.getProducerId());
                    } else {
                        Messagebox.show("Ο παραγωγός ανήκει σε άλλη ΔΑΟΚ");
                    }

                } else {
                    Messagebox.show(Labels.getLabel(LabelsEnum.NO_RESULTS.toString()));
                }

            } else { // εχει δωσει καποια γραμματα - ψαχνουμε στο επωνυμο
                List<Producer> producers = pdao.findBySurname(searchValue);
                if (!producers.isEmpty()) {
                    resultLstLml = new ListModelList<Producer>(producers);
                    resultLst.setModel(resultLstLml);
                } else {
                    resultLstLml = new ListModelList<Producer>(producers);
                    resultLst.setModel(resultLstLml);
                    Messagebox.show(Labels.getLabel(LabelsEnum.NO_RESULTS.toString()));
                }
            }
        }

    }

    @Listen("onSelect = #resultLst")
    public void onSelect() {
        //Messagebox.show("Eskasa !!");

//        Button btn = (Button) evt.getOrigin().getTarget();
//        Listitem litem = (Listitem) btn.getParent().getParent();
//
        //selectedTodo = todoListModel.getSelection().iterator().next();
        Producer producer = resultLstLml.getSelection().iterator().next();

        //    ((ListModelList) production.getListModel()).remove(product);
        //     products.remove(product) ;
        //    production.setModel(new ListModelList<Product>(products)); 
        Executions.sendRedirect("?pid=" + producer.getProducerId());

    }

}
